package domain;

import util.PhoneKind;


public class PhoneNumber {

	private int id;
	private String phoneKind;
	private String phoneNumber;
	private Contact contact;
	private long version;

	public PhoneNumber() {
		super();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getPhoneKind() {
		return phoneKind;
	}

	public void setPhoneKind(String phoneKind) {
		for (PhoneKind kind : PhoneKind.values()) {
			if (kind.toString().equals(phoneKind)) {
				this.phoneKind = kind.toString();
				return;
			}
		}
		// kind not found
		this.phoneKind = PhoneKind.fixe.toString();
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public long getVersion() {
		return version;
	}

	public void setVersion(long version) {
		this.version = version;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((contact == null) ? 0 : contact.getId());
		result = prime * result + id;
		result = prime * result
				+ ((phoneKind == null) ? 0 : phoneKind.hashCode());
		result = prime * result
				+ ((phoneNumber == null) ? 0 : phoneNumber.hashCode());
		result = prime * result + (int) (version ^ (version >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		PhoneNumber other = (PhoneNumber) obj;
		if (contact == null) {
			if (other.contact != null)
				return false;
		} 
		// we compare juste the contact id because the equals method create a loop
		else if (!(contact.getId() == other.contact.getId()))
			return false;
		if (id != other.id)
			return false;
		if (phoneKind == null) {
			if (other.phoneKind != null)
				return false;
		} else if (!phoneKind.equals(other.phoneKind))
			return false;
		if (phoneNumber == null) {
			if (other.phoneNumber != null)
				return false;
		} else if (!phoneNumber.equals(other.phoneNumber))
			return false;
		if (version != other.version)
			return false;
		return true;
	}
}
